/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newBugView.java
 *
 * Created on 16.01.2010, 22:41:21
 */

package jasebt_app;

import base.Bug;
import base.Tools;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import xmlparser.xmlWriter;
import org.jdesktop.application.Action;
/**
 *
 * @author Pavel Belov
 */
public class newBugView extends javax.swing.JDialog {

    SAXParser parser = null;
    public static String DBPATH = "DB" + File.separator;
    private static String creator = null;
    /** Creates new form newBugView */
    public newBugView(java.awt.Frame parent) throws SAXException, ParserConfigurationException {
        super(parent);
        initComponents();
        creator = Jasebt_bugView.currentUser.getLogin();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        parser = factory.newSAXParser();
        this.setTitle("Submit a new bug");

        cbProject.removeAllItems();
        
        for (int i = 0; i < Jasebt_bugView.project.length; i++) {
            cbProject.addItem(Jasebt_bugView.project[i].getName());
        }

        cbDevice.removeAllItems();

        for (int i = 0; i < Jasebt_bugView.device.length; i++) {
            cbDevice.addItem(Jasebt_bugView.device[i]);
        }
}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bClear = new javax.swing.JButton();
        fBugVer = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        cbPriority = new javax.swing.JComboBox();
        bCancel = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDescription = new javax.swing.JTextArea();
        cbProject = new javax.swing.JComboBox();
        fTitle = new javax.swing.JTextField();
        bSubmit = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        cbDevice = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jasebt_app.Jasebt_appApp.class).getContext().getResourceMap(newBugView.class);
        bClear.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        bClear.setText(resourceMap.getString("bClear.text")); // NOI18N
        bClear.setName("bClear"); // NOI18N
        bClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bClearActionPerformed(evt);
            }
        });

        fBugVer.setFont(resourceMap.getFont("fTitle.font")); // NOI18N
        fBugVer.setName("fBugVer"); // NOI18N

        jLabel20.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel20.setText(resourceMap.getString("jLabel20.text")); // NOI18N
        jLabel20.setName("jLabel20"); // NOI18N

        cbPriority.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        cbPriority.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3" }));
        cbPriority.setName("cbPriority"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jasebt_app.Jasebt_appApp.class).getContext().getActionMap(newBugView.class, this);
        bCancel.setAction(actionMap.get("closenewBugForm")); // NOI18N
        bCancel.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        bCancel.setText(resourceMap.getString("bCancel.text")); // NOI18N
        bCancel.setName("bCancel"); // NOI18N
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelActionPerformed(evt);
            }
        });

        jLabel6.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel16.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel16.setText(resourceMap.getString("jLabel16.text")); // NOI18N
        jLabel16.setName("jLabel16"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        taDescription.setColumns(20);
        taDescription.setFont(resourceMap.getFont("fTitle.font")); // NOI18N
        taDescription.setRows(5);
        taDescription.setName("taDescription"); // NOI18N
        jScrollPane1.setViewportView(taDescription);

        cbProject.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        cbProject.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Project 1", "Project 2", "Project 3" }));
        cbProject.setName("cbProject"); // NOI18N

        fTitle.setFont(resourceMap.getFont("fTitle.font")); // NOI18N
        fTitle.setName("fTitle"); // NOI18N

        bSubmit.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        bSubmit.setText(resourceMap.getString("bSubmit.text")); // NOI18N
        bSubmit.setName("bSubmit"); // NOI18N
        bSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSubmitActionPerformed(evt);
            }
        });

        jLabel5.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel15.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel15.setText(resourceMap.getString("jLabel15.text")); // NOI18N
        jLabel15.setName("jLabel15"); // NOI18N

        cbDevice.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        cbDevice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbDevice.setName("cbDevice"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(193, 193, 193))
                    .addComponent(fTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bSubmit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                        .addComponent(bClear, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbDevice, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(35, 35, 35))
                    .addComponent(jLabel16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(fBugVer)
                    .addComponent(cbProject, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel20)
                    .addComponent(jLabel15)
                    .addComponent(cbPriority, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbProject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbDevice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fBugVer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel20)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbPriority, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bSubmit)
                    .addComponent(bCancel)
                    .addComponent(bClear))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
}//GEN-LAST:event_bCancelActionPerformed
        // Submit a new bug
    private void bSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSubmitActionPerformed
        // Getting last bug number
        int bugCount = 0;

        bugCount = Tools.getLastBug();
        try {
            submitNewBug(bugCount);
            dispose();
        } catch (IOException ex) {
            Logger.getLogger(newBugView.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }//GEN-LAST:event_bSubmitActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void bClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bClearActionPerformed
        //TODO: needs implement
    }//GEN-LAST:event_bClearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancel;
    private javax.swing.JButton bClear;
    private javax.swing.JButton bSubmit;
    private javax.swing.JComboBox cbDevice;
    private javax.swing.JComboBox cbPriority;
    private javax.swing.JComboBox cbProject;
    private javax.swing.JTextField fBugVer;
    private javax.swing.JTextField fTitle;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea taDescription;
    // End of variables declaration//GEN-END:variables

    // TODO: needs implement
    private String getProjectManager(String project) {
        return "pbelov";
    }

    private boolean submitNewBug(int i) throws IOException {
        Bug bug = new Bug();
        bug.setTitle(fTitle.getText());
        bug.setDescription(taDescription.getText());
        bug.setProject(cbProject.getSelectedItem().toString());
        bug.setDevice(cbDevice.getSelectedItem().toString());
        bug.setBugVer(fBugVer.getText());

        // assigning bug to Project Manager.
        String projectManager = getProjectManager(bug.getProject());
        bug.setAssignee(projectManager);
        bug.setTester(creator); 
        bug.setCreator(creator);

        bug.setPriority(Integer.parseInt(cbPriority.getSelectedItem().toString()));

        bug.setFixNumber(0);
        bug.setFixVer("");

        // Set state "Open" / "New"
        bug.setState(0);

        bug.setID(i);

        return saveBugFile(bug);
    }

    private boolean saveBugFile(Bug bug) throws IOException {
        boolean res = true;
        xmlWriter xmlw = new xmlWriter(Tools.getFilePath(bug.getID()));
        xmlw.createFile(bug);
        return res;
    }

    @Action public void closenewBugForm() {
        dispose();
    }
}
